﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Threading;

namespace ISUM_V2
{
    class MorseCodeFileModule
    {
        private static MorseCodeFileModule _MorseCodeFM = null;
        private bool IsCanSave = false;

        private MorseCodeFileModule() { }

        public static MorseCodeFileModule getInstance()
        {
            if (_MorseCodeFM == null)
            {
                _MorseCodeFM = new MorseCodeFileModule();
            }

            return _MorseCodeFM;
        }

        public void writeFile(string fileName, Object obj)
        {

            try
            {

                Stream stm = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);

                BinaryFormatter bf = new BinaryFormatter();

                bf.Serialize(stm, obj);//arr은 어레이 리스트

                stm.Close();


                Console.WriteLine("파일쓰기성공");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "파일 입출력 에러");
            }



            //Thread t = new Thread( new ParameterizedThreadStart(  Write(fileName,obj) )  );

        }
        
    
        private void Write(string fileName, Object obj)
        {
            try
            {

                Stream stm = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);

                BinaryFormatter bf = new BinaryFormatter();

                bf.Serialize(stm, obj);//arr은 어레이 리스트

                stm.Close();


                Console.WriteLine("파일쓰기성공");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "파일 입출력 에러");
            }

        }

        public Object readFile(string str)
        {
            Object obj = null;
            try
            {
                

                String fileDir = str;

                Console.WriteLine(fileDir);

                Stream stm = File.Open(fileDir, FileMode.Open, FileAccess.Read);

                BinaryFormatter bf = new BinaryFormatter();

                obj = (Object)bf.Deserialize(stm);

                stm.Close();

                return obj;
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return obj;
        }
    }
}
